package io.coderyeah.basic.controller;

import io.coderyeah.basic.Result;
import io.coderyeah.basic.dto.LoginDto;
import io.coderyeah.basic.exception.BusinessException;
import io.coderyeah.user.dto.BinderDTO;
import io.coderyeah.user.service.LoginInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
@Api("登录接口")
public class LoginController {
    @Autowired
    private LoginInfoService loginInfoService;

    @PostMapping("/account")
    @ApiOperation("账号登录")
    public Result accountLogin(@RequestBody LoginDto loginDto) {
        try {
            Map<String, Object> map = loginInfoService.accountLogin(loginDto);
            return Result.success(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统繁忙，请稍后重试!!!");
        }
    }

    @GetMapping("/wechat/{code}")
    @ApiOperation("微信登录")
    public Result wechatLogin(@PathVariable("code") String code) {
        try {
            return loginInfoService.wechatLogin(code);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统繁忙，请稍后重试!!!");
        }
    }

    @PostMapping("/wechat/binder")
    @ApiOperation("微信绑定授权")
    public Result wechatBinder(@RequestBody BinderDTO binderDTO) {
        try {
            return loginInfoService.wechatBinder(binderDTO);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统繁忙，请稍后重试!!!");
        }
    }
}
